package com.gton.lpl.common.pojo;

import com.baomidou.mybatisplus.core.toolkit.AES;
import com.gton.lpl.common.tools.RSAUtil;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @description: 加密摘要, 加密规则
 * @author: GuoTong
 * @createTime: 2021-10-05 17:20
 * @since JDK 1.8 OR 11
 **/
@Component
public class ThisSystemSign {
    // 生成 16 位随机 AES 密钥
    private String randomKey = "4226dc81fed64f3d";

    /**
     * Description:  签名：前端传入参数加密
     * @author: GuoTong
     */
    public String getThisUserKey(String msg) {
        try {
            return RSAUtil.encryptByPublicKey(RSAUtil.pubKey, msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "数据加密报错，传入参数有误";
    }

    /**
    * Description:  签名：数据库层次实际加密，不可逆
    * @author: GuoTong
    */
    public String getDataSignByDb(String msg) {
        String data = msg;
        // 随机密钥加密
        String result = AES.encrypt(data, randomKey);
        if (result.contains("=")) {
            result = result.replace("=", "");
        }
        return result;
    }

    /**
     * Description:  签名：数据库层次实际解密
     * @author: GuoTong
     */
    public String getDataSignByDbCallBack(String msg) {
        String data = msg+"==";
        // 随机密钥加密
        String result = AES.decrypt(data, randomKey);
        if (result.contains("=")) {
            result = result.replace("=", "");
        }
        return result;
    }

    public static void main(String[] args) {
        // 数据库密码加密实际操作
        String randomKey2 = AES.generateRandomKey();
        System.out.println(randomKey2);
        String admin = AES.encrypt("root", randomKey2);
        System.out.println(admin);
        //获取这周是星期几
        LocalDateTime now = LocalDateTime.now();
        int dayOfMonth = now.getDayOfMonth();
        System.out.println(dayOfMonth);
    }

}
